#include "Lexer.h"
#include "Parser.h"
#include "ExecutionPlan.h"
#include <iostream>
#include <vector>
#include <memory>

using namespace MiniDB;

void printResult(const std::vector<std::unordered_map<std::string, std::variant<std::string, int, double>>>& results) {
    std::cout << "查询结果:" << std::endl;
    std::cout << "共找到 " << results.size() << " 条记录" << std::endl;

    for (const auto& row : results) {
        std::cout << "{ ";
        for (auto it = row.begin(); it != row.end(); ++it) {
            if (it != row.begin()) std::cout << ", ";
            std::cout << it->first << ": ";

            if (std::holds_alternative<std::string>(it->second)) {
                std::cout << "\"" << std::get<std::string>(it->second) << "\"";
            } else if (std::holds_alternative<int>(it->second)) {
                std::cout << std::get<int>(it->second);
            } else if (std::holds_alternative<double>(it->second)) {
                std::cout << std::get<double>(it->second);
            }
        }
        std::cout << " }" << std::endl;
    }
    std::cout << std::endl;
}

void testWhereConditions() {
    std::cout << "=== 测试WHERE条件查询功能 ===" << std::endl;

    // 测试用例1: 简单的等式条件
    std::cout << "测试1: SELECT * FROM users WHERE age = 25" << std::endl;
    {
        std::string sql = "SELECT * FROM users WHERE age = 25;";
        Lexer lexer(sql);
        auto tokens = lexer.tokenize();

        Parser parser(tokens);
        auto statements = parser.parse();

        if (!statements.empty()) {
            auto select_stmt = std::dynamic_pointer_cast<SelectStatement>(statements[0]);
            if (select_stmt) {
                auto results = executeSelectQuery(select_stmt);
                printResult(results);
            }
        }
    }

    // 测试用例2: 大于条件
    std::cout << "测试2: SELECT * FROM users WHERE age > 20" << std::endl;
    {
        std::string sql = "SELECT * FROM users WHERE age > 20;";
        Lexer lexer(sql);
        auto tokens = lexer.tokenize();

        Parser parser(tokens);
        auto statements = parser.parse();

        if (!statements.empty()) {
            auto select_stmt = std::dynamic_pointer_cast<SelectStatement>(statements[0]);
            if (select_stmt) {
                auto results = executeSelectQuery(select_stmt);
                printResult(results);
            }
        }
    }

    // 测试用例3: 字符串比较
    std::cout << "测试3: SELECT * FROM users WHERE name = 'Alice'" << std::endl;
    {
        std::string sql = "SELECT * FROM users WHERE name = 'Alice';";
        Lexer lexer(sql);
        auto tokens = lexer.tokenize();

        Parser parser(tokens);
        auto statements = parser.parse();

        if (!statements.empty()) {
            auto select_stmt = std::dynamic_pointer_cast<SelectStatement>(statements[0]);
            if (select_stmt) {
                auto results = executeSelectQuery(select_stmt);
                printResult(results);
            }
        }
    }

    // 测试用例4: AND条件
    std::cout << "测试4: SELECT * FROM users WHERE age > 20 AND id < 3" << std::endl;
    {
        std::string sql = "SELECT * FROM users WHERE age > 20 AND id < 3;";
        Lexer lexer(sql);
        auto tokens = lexer.tokenize();

        Parser parser(tokens);
        auto statements = parser.parse();

        if (!statements.empty()) {
            auto select_stmt = std::dynamic_pointer_cast<SelectStatement>(statements[0]);
            if (select_stmt) {
                auto results = executeSelectQuery(select_stmt);
                printResult(results);
            }
        }
    }

    // 测试用例5: 投影特定列
    std::cout << "测试5: SELECT name, age FROM users WHERE age >= 25" << std::endl;
    {
        std::string sql = "SELECT name, age FROM users WHERE age >= 25;";
        Lexer lexer(sql);
        auto tokens = lexer.tokenize();

        Parser parser(tokens);
        auto statements = parser.parse();

        if (!statements.empty()) {
            auto select_stmt = std::dynamic_pointer_cast<SelectStatement>(statements[0]);
            if (select_stmt) {
                auto results = executeSelectQuery(select_stmt);
                printResult(results);
            }
        }
    }

    // 测试用例6: 不等于条件
    std::cout << "测试6: SELECT * FROM users WHERE age != 25" << std::endl;
    {
        std::string sql = "SELECT * FROM users WHERE age != 25;";
        Lexer lexer(sql);
        auto tokens = lexer.tokenize();

        Parser parser(tokens);
        auto statements = parser.parse();

        if (!statements.empty()) {
            auto select_stmt = std::dynamic_pointer_cast<SelectStatement>(statements[0]);
            if (select_stmt) {
                auto results = executeSelectQuery(select_stmt);
                printResult(results);
            }
        }
    }

    // 测试用例7: 小于等于条件
    std::cout << "测试7: SELECT * FROM users WHERE age <= 25" << std::endl;
    {
        std::string sql = "SELECT * FROM users WHERE age <= 25;";
        Lexer lexer(sql);
        auto tokens = lexer.tokenize();

        Parser parser(tokens);
        auto statements = parser.parse();

        if (!statements.empty()) {
            auto select_stmt = std::dynamic_pointer_cast<SelectStatement>(statements[0]);
            if (select_stmt) {
                auto results = executeSelectQuery(select_stmt);
                printResult(results);
            }
        }
    }

    // 测试用例8: 无WHERE条件
    std::cout << "测试8: SELECT * FROM users (无WHERE条件)" << std::endl;
    {
        std::string sql = "SELECT * FROM users;";
        Lexer lexer(sql);
        auto tokens = lexer.tokenize();

        Parser parser(tokens);
        auto statements = parser.parse();

        if (!statements.empty()) {
            auto select_stmt = std::dynamic_pointer_cast<SelectStatement>(statements[0]);
            if (select_stmt) {
                auto results = executeSelectQuery(select_stmt);
                printResult(results);
            }
        }
    }
}

int main() {
    try {
        testWhereConditions();
        std::cout << "所有WHERE条件测试完成！" << std::endl;
    } catch (const std::exception& e) {
        std::cerr << "测试过程中发生错误: " << e.what() << std::endl;
        return 1;
    }

    return 0;
}
